package cn.bgs.controller;

import cn.bgs.Exception.PhoenixException;
import cn.bgs.pojo.ClassRoom;
import cn.bgs.pojo.Student;
import cn.bgs.pojo.VoAll.ClassRoomVo;
import cn.bgs.service.RoomService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author Mr.JIA
 * @DATE 2021/7/26 0026 9:37
 * @PROJECT_NAME mybatis_phoenix
 * @PACKAGE_NAME cn.bgs.controller
 */
@Controller
@RequestMapping("/room")
public class RoomController {

    Logger log = LogManager.getLogger();

    @Autowired
    private RoomService roomService;
    //跳转
    @RequestMapping("/sel")
    public String sel(){
        return "/view/classroom";
    }

    //查询 区域 area
    @RequestMapping("/findArea")
    @ResponseBody
    public List<ClassRoom> findArea(){
        return roomService.findArea();
    }
    //查询 楼号 buildingNo
    @RequestMapping("/findBuildingNo")
    @ResponseBody
    public List<ClassRoom> findBuildingNo(@RequestBody String aa){
        JSONObject jsonObject = JSONObject.parseObject(aa);
        String buildingNo = jsonObject.getString("no");
        log.info("buildingNo:::::::"+buildingNo);
        return roomService.findBuildingNo(buildingNo);
    }
    //查询 楼层 buildingFloor
    @RequestMapping("/findBuildingFloor")
    @ResponseBody
    public List<ClassRoom> findBuildingFloor(@RequestBody String bb){
        JSONObject jsonObject = JSONObject.parseObject(bb);
        String no = jsonObject.getString("no");
        String floor = jsonObject.getString("floor");
        log.info("buildingNo=======:::::::"+no);
        log.info("buildingFloor======:::::::"+floor);
        return roomService.findBuildingFloor(floor,no);
    }


   //修改 room
    @RequestMapping("/updateRoom")
    @ResponseBody
    public Map<String,Object> updateRoom(@RequestBody String roo){
        Map<String, Object> map = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(roo);
        ClassRoom cc = new ClassRoom();
        cc.setPkId(Integer.parseInt(jsonObject.getString("pkId")));
        cc.setArea(jsonObject.getString("area"));
        cc.setBuildingNo(jsonObject.getString("buildingNo"));
        cc.setBuildingFloor(jsonObject.getString("buildingFloor"));
        cc.setClassroomId(jsonObject.getString("classroomId"));
        cc.setClassroomNumber(jsonObject.getString("classroomNumber"));
        cc.setClassroomName(jsonObject.getString("classroomName"));
        cc.setGalleryful(Integer.parseInt(jsonObject.getString("galleryful")));
        cc.setUseStatus(jsonObject.getString("userStatus"));
        cc.setRemark(jsonObject.getString("remark"));
        Calendar now = Calendar.getInstance();
        cc.setUpdateTime(now.getTime());

        try{
            int a = roomService.updateRoom(cc);
            map.put("data",a);
            map.put("code","0000");
            map.put("msg","修改成功");
        }catch (PhoenixException p){
            log.error("code",p.getCode());
            log.error("msg",p.getMsg());
            log.error("exception"+p.getMessage());
        }
        return map;

    }


    //删除 room
    @RequestMapping("/delRoom")
    @ResponseBody
    public Integer delRoom(@RequestBody String ro){
        JSONObject jsonObject = JSONObject.parseObject(ro);
        int pkId = Integer.parseInt(jsonObject.getString("pkId"));
        log.info("pkid===========:::::::::::::"+pkId);
        return roomService.delRoom(pkId);
    }
    //添加 room
    @RequestMapping("/addRoom")
    @ResponseBody
    public Integer addRoom(@RequestBody String addRoom){
        JSONObject jsonObject = JSONObject.parseObject(addRoom);
        ClassRoom ro = new ClassRoom();
        ro.setArea(jsonObject.getString("area"));
        ro.setBuildingNo(jsonObject.getString("buildingNo"));
        ro.setBuildingFloor(jsonObject.getString("buildingFloor"));
        ro.setClassroomId(jsonObject.getString("classroomId"));
        ro.setClassroomNumber(jsonObject.getString("classroomNumber"));
        ro.setClassroomName(jsonObject.getString("classroomName"));
        ro.setGalleryful(Integer.parseInt(jsonObject.getString("galleryful")));
        ro.setUseStatus(jsonObject.getString("useStatus"));
        ro.setRemark(jsonObject.getString("remark"));
        ro.setDeleteFlag(0);
        Calendar now = Calendar.getInstance();//获取时间
        ro.setCreateTime(now.getTime());
        ro.setUpdateTime(now.getTime());
        return roomService.addRoom(ro);
    }

    //全查
    @ResponseBody
    @RequestMapping("/findAllRoom")
    public Map<String,Object> findAllStu(@RequestParam(required=false,defaultValue="1") int page,
                                         @RequestParam(required=false,defaultValue="7") int limit,
                                         ClassRoomVo vo){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
//        List<Student> stu  = stuService.findAllStu(stuName,mobile,sex,age,idNum);// 这是我们的sql
//        for(Student i:stu){
//            i.setNativePlace(i.getNativePlace());
//        }

        log.info("String classroomNumber, String classroomName, Integer galleryfulMin, Integer galleryfulMax, String araea, String buildingNo, String buildingFloor：：：：：：："
                +vo.getClassroomNumber()+"   "
                +vo.getClassroomName()+"   "
                +vo.getGalleryfulMin()+"   "
                +vo.getGalleryfulMax()+"   "
                +vo.getArea()+"   "
                +vo.getBuildingNo()+"   "
                +vo.getBuildingFloor()+"   "
        );
        List<Student> grade = roomService.findAllRoom(vo);
        log.info("data======::::::::"+grade);
        // 使用pageInfo包装查询
        PageInfo<Student> rolePageInfo = new PageInfo<>(grade);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }

}
